(function(jQuery){
	//获取当前页面的地址并取出当前商品id
    function getUrl(){
        var param=location.search;

        return param.split("=")[1];
    }
    //点击时渲染购物车数据
    function getCount(obj){
        if(sessionStorage.getItem("userInfo")==null){
            let cookieList=JSON.parse($.cookie("carts")||'[]');

            if(cookieList.length==0){
                obj.html(0);
            }else{
                let count=0;
                cookieList.forEach((ele,index)=>{
                    count+=ele.num;
                })
                obj.text(count);
            }
        }else{
            let oUser=JSON.parse(sessionStorage.getItem("userInfo"));
            $.ajax({
                url:"../../server/getCartById.php",
                type:"post",

                data:{u_id:Number(oUser.u_id)}
            }).then(function (res) {

                if(res.length>=1){
                    let count=0;
                    res.forEach((ele,index)=>{
                        count+=ele.c_num;
                    })

                    obj.text(count);
                }else{
                    obj.html(0);
                }
            })
        }
    }
    //动态添加商品属性
    $(function () {
        $.ajax({
            url:"../../server/getlistById.php",
            data:{"gid":getUrl()}
        }).then(function (res) {
            $(".bd h3").html(res.goodsName);
            $(".contain img").attr("src",res.img[0]);
            $(".show img").attr("src",res.img[0]);
            $(".con_right li:first").children("em").html(res.newPrice);
            $(".con_right li:first").children("span").html(res.discount);
            $(".con_right li:first").children("b").html(res.oldPrice);
            $(".candidates img").eq(0).attr("src",res.img[0]);
			$(".price_buy dl").eq(0).find("span").html("￥"+res.oldPrice);
            $(".price_buy dl").eq(1).find("dd").html(res.discount);
            $(".price_buy dl").eq(2).find("dd").html("￥"+(Number(res.oldPrice)-Number(res.newPrice)));
            $(".price").html("￥"+res.newPrice);
            $(".gw").attr("data-info",JSON.stringify(res));
            $(".candidates img").each(function () {
                $(this).attr("src",res.img[$(this).index()]);
            })
        })
    })
	//加入购物车
	$(function () {
		$(document).on("click",".gw",function () {
			let currentObj=$(this).data("info");
			$cloneImg=$(".contain").clone(true);
            $imgPosition=$(".contain").offset();
            $imgPosition.position="absolute";
            $imgPosition.width="280px";
            $imgPosition.height="280px";
            $imgPosition.left="0px";
            $imgPosition.top="0px";
            $cloneImg.css($imgPosition);
            $(".bd").append($cloneImg);
            $target=$(".rightbar .tocart").offset();
            //console.log($target);
            $target.height=0;
            $target.width=0;
            console.log($cloneImg.offset());
            $cloneImg.animate({}).animate($target,500,function () {
                $(this).remove();
            })
            if(sessionStorage.getItem("userInfo")==null){
            	let cookieList=JSON.parse($.cookie("carts")||'[]');
            	let flag=cookieList.some((ele,index)=>{
            		if(ele.gid==currentObj.gid){
            			cookieList[index].num=Number(cookieList[index].num)+1;
            			return true;
					}else{
            			return false;
					}
				})
				if(flag!=true){
            		currentObj.num=1;
            		cookieList.push(currentObj);
				}
				$.cookie("carts",JSON.stringify(cookieList));
                getCount($(".rightbar .count"));
                getCount($(".topright .count"));

			}else{
            	let oUser=JSON.parse(sessionStorage.getItem("userInfo"));
            	let obj={};
            	currentObj.u_id=oUser.u_id;
            	currentObj.num=1;
            	currentObj.img=currentObj.img[0];
               //currentObj.newPrice=Number(obj.newPrice.split("￥")[1]);
				for(var key in currentObj){
					obj[key]=currentObj[key];
				}
				obj.newPrice=Number(obj.newPrice);
                $.ajax({
					url:"../../server/addCart.php",
					type:"post",
					data:obj,
				}).then(function (res) {
                    console.log(res);
                    if(res.status==1){
                    	//location.reload();
                        getCount($(".rightbar .count"));
                        getCount($(".topright .count"));
					}
                })
			}
        })
    })
	//tab栏切换以及放大镜
	$(function(){
		//tab栏切换
		$(".stuff_tab li").click(function(){
			let i=$(this).index();
			$(this).attr("class","current").siblings("li").attr("class","");
			$(".box_content").eq(i).css("display","block").siblings(".box_content").css("display","none");
		})
		//商品放大镜
		//console.log($(".contain"));
		var $sImg=$(".contain img");
		var $mask=$(".mask");
		var $showBox=$(".show");
		var $showImg=$(".show img");
		
		var $sImgW=$sImg.width();
		var $showBoxW=$showBox.width();
		var $showImgW=$showImg.width();
		//console.log($sImgW,$showBoxW,$showImgW);
		//得到遮罩板的宽高
		var $sW=$sImgW/$showImgW*$showBoxW;
		$mask.css("width",$sW+"px").css("height",$sW+"px");
		var $scale=$showImgW/$sImgW;
		//鼠标进入遮罩板显示，显示大图的盒子显示
		$(".contain").mouseenter(function(){

            $mask.show();
			$showBox.show();
			$(document).mousemove(function(evt){
				var e=evt||window.event;
				var $disX=e.clientX-$(".contain").offset().left-$mask.width()/2+$(document).scrollLeft();
				var $disY=e.clientY-$(".contain").offset().top-$mask.height()/2+$(document).scrollTop();
				if($disX<=0){
					$disX=0;
				}
				if($disY<=0){
					$disY=0;
				}
				if($disX>=$(".contain").width()-$mask.width()){
					$disX=$(".contain").width()-$mask.width();
				}
				if($disY>=$(".contain").height()-$mask.height()){
					$disY=$(".contain").height()-$mask.height();
				}
				//console.log($disX,$disY);
				$mask.css("left",$disX+"px").css("top",$disY+"px");
				$showImg.css("left",-$disX*$scale+"px").css("top",-$disY*$scale+"px");
			})
		}).mouseleave(function(){
			$(document).unbind("mousemove");
			$mask.hide();
			$showBox.hide();
		})
		var $caImg=$(".candidates img");

		$caImg.click(function(){
			$(this).addClass("current").siblings("img").removeClass("current");
			//console.log($(this).siblings("img"));
			//console.log($caImg);
			var $src=$(this).attr("src");
			$sImg.attr("src",$src);
			$showImg.attr("src",$src);
		})

	})
	//倒计时
	$(function () {
		let date=new Date();
        let date2=new Date("2019-1-1");
		let timeLength=Math.abs(date2.valueOf(date2)-date.valueOf(date));
		let num=0;
		setInterval(function () {
			num++;
			let count=timeLength-num;
			let dd=parseInt(count/1000/60/60/24);
			dd=dd<=99?"0"+dd:dd;
            let hh=parseInt(count/1000/60/60)%24;
            hh=hh<=9?"0"+hh:hh;
            let mm=parseInt(count/1000/60)%60;
            mm=mm<=9?"0"+mm:mm;
            let ss=parseInt(count/1000)%60;
            ss=ss<=9?"0"+ss:ss;
            let ms=parseInt(count%1000/100);
            $(".time_end .days").html(dd);
            $(".time_end .hours").html(hh);
            $(".time_end .minutes").html(mm);
            $(".time_end .seconds").html(ss+"."+ms);
        },1)
    })
})(jQuery)
